<?php
if (!defined('CURRENT_PATH')){
	die('Uncommitted visit');
}
$siteConfig=spClass(site_config);
switch($mod){
/*================================= 功能设置 ======================================*/	
case 'setting':	
$datil=$siteConfig->findAll();
//dump($datil);
$this->datil=$datil;
$this->sub=1;	
$this->display('admin/index_setting.html');	
break;
case 'settingAction':
$tempArr=$_POST;
$mapArr=array('sitename'=>'1','sitetitle'=>'2','meta_k_index'=>'12','meta_d_index'=>'13','meta_k_all'=>'14','meta_d_all'=>'15','visit'=>'8','contact'=>'11','icp'=>'24');
$sql.='UPDATE `site_config` SET `value`=case id '.$this->sqlGen1($mapArr,$tempArr,'value').'END';
$siteConfig->runsql($sql);
$this->success("恭喜，操作成功");
break;
/*================================= 管理员设置 ======================================*/	
case 'manageset':
$adminlist=spClass(manage);
$this->sub=2;
$res=$adminlist->findAll('id!=1','system DESC,inttime DESC','id,username,name,auth,system,inttime','');
$admin_options=array(0=>'---请选择---');
foreach($res as $id => $v){
	$res[$id]['auth']=unserialize($v['auth']);
	if($v['system']!=1)$admin_options[$v['id']]=$v['username'];
}
$this->admin_options = $admin_options;
$this->admin_id=0;
$this->res=$res;
$this->display('admin/index_manageset.html');
break;
/*================================= 密码修改 ======================================*/		
case 'passChangeAction':
if($this->spArgs(Pass)!=null&&$this->spArgs(NewPass)==$this->spArgs(ReNewPass)&&strlen($this->spArgs(NewPass))>=6){
	if($this->spArgs(Pass)==$this->spArgs(ReNewPass)){
		$msg='您输入的旧密码与新密码相同！';
	}else{
		$PassEdit=spClass(manage);
		$P=$PassEdit->find($conditions=array('id'=>$_SESSION['id']),'',$fields='password');
		$sigPassword=$P[password];
		if($this->chkpass($this->spArgs('Pass'),$sigPassword)){
			$newPass= $this->passgen($this->spArgs(NewPass));
			$PassEdit->update($conditions=array('id'=>$_SESSION['id']),$row=array('password'=>$newPass));
			$msg='恭喜密码更改成功，请牢记您的新密码！';
			$this->success($msg,$url='./index.php?c=admin&a=logout');
			die;
		}else{
			$msg='旧密码输入有误!';
		}
	}
}else{
		if($this->spArgs(Pass)==null){
			$msg='请填写旧密码!';
		}else if(strlen($this->spArgs(NewPass))<6){
			$msg='新密码长度不得小于6!';		
		}else{
			$msg='两次输入的密码不相同!';
		}
}
$this->error($msg);
break;
/*================================= 添加管理员 ====================================*/
case adminAddAction:
if($this->spArgs(User)!=null&&$this->spArgs(Name)!=null&&$this->spArgs(Pass)==$this->spArgs(RePass)&&strlen($this->spArgs(Pass))>=6){
	$adminedit=spClass(manage);
	$saveMapArr=array('User'=>'username','Name'=>'name','Pass'=>'password',);
	//检查用户名是否存在
	$res=$adminedit->find($conditions=array('username'=>$this->spArgs(User)),'',$fields='1');
	if($res){
		$msg='该用户名已经存在！';
		$this->error($msg);
		die;
	}else{	
	//dump($this->spArgs());	
		$this->DataToSave($saveMapArr,$this->spArgs(),$newrow=array('id'=>null,'system' =>'0','sessid'=>'','auth'=>'','inttime'=>time()));
		foreach($saveMapArr as $id=>$v){
			$tempArr=array($v=>$this->spArgs($id));
			$newrow=array_merge($newrow,$tempArr);
		}
		/*------------密码生成---------------*/
		$newrow['password']= $this->passgen($newrow['password']);
		/*-----------------------------------*/
		$adminedit->create($newrow);
		$this->success('添加管理员成功！',$url='./index.php?c=admin&a=index&mod=manageset#tab03');		
	}
	}else{
		if($this->spArgs(User)==null||$this->spArgs(Name)==null){
			$msg='用户名及姓名不得为空！';
		}else if(strlen($this->spArgs(Pass))<6){
			$msg='密码长度不得小于六位';
		}else if($this->spArgs(Pass)!=$this->spArgs(RePass)){
			$msg='两次输入密码不同';
		}
		$this->error($msg,$url='./index.php?c=admin&a=index&mod=manageset#tab02');
	}	
break;
/*===============================  删除管理员 ====================================*/
case adminDelAction:
$ad=spClass('manage');
$id=intval($_REQUEST['id']);
$res=$ad->find($conditions=array('id'=>$id),$sort = null, $fields ='system');
if(!$res||$id==1){
	$this->error("用户不存在！",$url='./index.php?c=admin&a=index&mod=manageset#tab03');
}else if($res[system]!=1){
	$ad->delete($conditions=array('id'=>$id));
	$this->success("恭喜，管理员删除成功",$url='./index.php?c=admin&a=index&mod=manageset#tab03');	 
}else{
	$this->error("不能删除系统管理员",$url='./index.php?c=admin&a=index&mod=manageset#tab03'); 
}
break;
/*===============================  权限管理 ====================================*/
case authChangeAction:
$aa=spClass('manage');
$uid=intval($_REQUEST['admin_id']);
$res=$aa->find($conditions=array('id'=>$uid),$sort = null, $fields ='system');
if(!$res||$id==1){
	$this->error("管理员账户不存在！",$url='./index.php?c=admin&a=index&mod=manageset#tab03');
}else if($res[system]!=1){
	$authArray=array('a_0','a_1','a_2','a_3','a_4','a_5','a_6','a_7');
	foreach($authArray as $id=> $v){
		if ($this->spArgs($v)=='on'){
			$auth[$id]=1;		
			}else{
			$auth[$id]=0;	
			}
		}
	$aa->updateField($conditions=array('id'=>$uid), $field='auth',$value=serialize($auth));
		$this->success("恭喜，管理权限修改成功",$url='./index.php?c=admin&a=index&mod=manageset#tab03');
}else{
	$this->error("系统管理员权限无需修改！",$url='./index.php?c=admin&a=index&mod=manageset#tab03'); 
}
break;
case advertisement:
/*================================= 广告设置 ======================================*/	
$this->sub=3;
$ip=spClass(img_player);
$res=$ip->findAll($conditions=array('fid'=>1));//
$ad_options=array(0=>'---请选择---');
foreach($res as $id => $v){
	$ad_options[$v['id']]=$v['upload_discription'];
}
$this->ad_options = $ad_options;
$this->ad_id=0;
$this->res=$res;
$this->display('admin/index_adset.html');
break;
default:
case adStatus:
/*================================= 广告状态 ======================================*/	
$status=($_REQUEST['show']=="yes")?1:0;
$id=intval($_REQUEST['id']);
$this->showstatus('img_player',$id,$status,false);
break;
default:
/*================================= 首页 ======================================*/
$this->sub=0;
$this->display('admin/index_main.html');
	



}
die;